package com.kewo.listviewreturnfirst;

import android.animation.ValueAnimator;
import android.content.Context;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements AbsListView.OnScrollListener {

    ListView mListView;
    FloatingActionButton mActionButton;
    boolean mbIsVisible = false;
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = MainActivity.this;
        initView();
        initdata();
    }

    private void initView() {
        mListView = (ListView) findViewById(R.id.my_listView);
        mActionButton = (FloatingActionButton) findViewById(R.id.floatingBtn);
    }

    private void initdata() {
        List<String> data = new ArrayList<>();
        for (int i = 0; i < 40; i++) {
            data.add(String.valueOf(i));
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.activity_list_item, android.R.id.text1, data);
        mListView.setAdapter(adapter);

        mListView.setOnScrollListener(this);
        mActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mListView.smoothScrollToPosition(0);
            }
        });
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int i) {

    }


    /**
     * firstVisibleItem 表示在当前屏幕显示的第一个listItem在整个listView里面的位置（下标从0开始）
     * visibleItemCount表示在现时屏幕可以见到的ListItem(部分显示的ListItem也算)总数
     * totalItemCount表示ListView的ListItem总数
     */
    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        if (totalItemCount > visibleItemCount) {
            if (!mbIsVisible) {
                if (mListView.getFirstVisiblePosition() == 0) {
                    ValueAnimator valueAnimatorBar = ValueAnimator.ofInt(dip2px(mContext, 80), 0);
                    valueAnimatorBar.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                        @Override
                        public void onAnimationUpdate(ValueAnimator valueAnimator) {
                            //获取当前的height值
                            int h = (Integer) valueAnimator.getAnimatedValue();
                            //动态更新view的高度
                            mActionButton.getLayoutParams().height = h;
                            mActionButton.getLayoutParams().width = h;

                            mActionButton.requestLayout();
                        }
                    });
                    valueAnimatorBar.setDuration(300);
                    //开始动画
                    valueAnimatorBar.start();
                    mbIsVisible = !mbIsVisible;
                }
            } else {
                if (mListView.getFirstVisiblePosition() > 0) {
                    mActionButton.setVisibility(View.VISIBLE);
                    ValueAnimator valueAnimatorBar = ValueAnimator.ofInt(0, dip2px(mContext, 80));
                    valueAnimatorBar.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                        @Override
                        public void onAnimationUpdate(ValueAnimator valueAnimator) {
                            //获取当前的height值
                            int h = (Integer) valueAnimator.getAnimatedValue();
                            //动态更新view的高度
                            mActionButton.getLayoutParams().height = h;
                            mActionButton.getLayoutParams().width = h;

                            mActionButton.requestLayout();
                        }
                    });
                    valueAnimatorBar.setDuration(300);
                    //开始动画
                    valueAnimatorBar.start();
                    mbIsVisible = !mbIsVisible;
                }
            }
        } else {

            mActionButton.setVisibility(View.GONE);
        }
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
